using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace SpiralOut.CustomActions
{
    class ListOrder : SPFeatureReceiver
    {
        private const string cfldOrder = "Order";
        private const string csviewOrder = "By Order";
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
           
            SPWeb web = (SPWeb)properties.Feature.Parent;            
            for(int i=0; i<web.Lists.Count; i++)
              {  
                  try
                  {
                      SPList list = web.Lists[i];
                      if ((list.BaseTemplate == SPListTemplateType.GenericList) || (list.BaseTemplate == SPListTemplateType.Tasks) || (list.BaseTemplate == SPListTemplateType.GanttTasks) || (list.BaseTemplate == SPListTemplateType.Contacts))
                    {
                        if (list.Fields.ContainsField(cfldOrder))
                        {
                           SPField field = list.Fields[cfldOrder];
                            field.SchemaXml = field.SchemaXml.Replace("Hidden=\"TRUE\"","Hidden=\"FALSE\"");
                            field.ShowInListSettings = false;
                            field.Update();
                           SPView view =null;
                            foreach (SPView tempview in list.Views)
                              if(tempview.Title.Equals(csviewOrder))
                                view =tempview;
                            if(view ==null)
                                view = list.DefaultView.Clone(csviewOrder, 100, true, false);
                            view.Query = "<OrderBy><FieldRef Name ='Order'/></OrderBy>";
                            view.Update();
                            
                        }

                    }
                }
                catch { }  
                             
            }           
                   
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
           // throw new Exception("The method or operation is not implemented.");
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }
    }
}
